package com.lwc.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.lang.invoke.MutableCallSite;
import java.lang.reflect.Type;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.UUID;

@RestController
public class UpdateController {
    @PostMapping("/upload")
    public  String upload(MultipartFile file, HttpServletRequest request){
        return toUpload(file, request,null);
    }

    @PostMapping("/batch/upload")
    public List batchUpdate(List <MultipartFile> files,HttpServletRequest request){

List <String>list=new ArrayList<>();
        files.forEach(file->{
            String tmp=toUpload(file, request,null);
list.add(tmp);
        });
return list;
    }
//上传
// 单个文件

    String toUpload(MultipartFile file, HttpServletRequest request, String type){
      FilePath filePath=initDir(request,type);
        String newFile=getNewFileName(file);

        try {
            file.transferTo(new File(filePath.getRoot()+"\\"+newFile));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return filePath.getDir()+ newFile;
    }
//定义目录
    private  final String BASEDIR="attchment";//所有上传文件的根目录

//初始目录

FilePath initDir(HttpServletRequest request,String type){
    String urlPath=BASEDIR+"/"+type+"/"+gesystemYMD()+"/";
if (null==type||"".equals(type))

type="image";
        String root=request.getServletContext().getRealPath("/")+urlPath;

        File dir=new File(root);

        if (!dir.exists())
            dir.mkdirs();//创建多层目录
return new FilePath(dir.getAbsolutePath(),urlPath);
    }


    class  FilePath{
        String root;
        String dir;

        public FilePath(String root, String dir) {
            this.root = root;
            this.dir = dir;
        }


        public String getRoot() {
            return root;
        }

        public void setRoot(String root) {
            this.root = root;
        }

        public String getDir() {
            return dir;
        }

        public void setDir(String dir) {
            this.dir = dir;
        }
    }

//获取系统时间
    String gesystemYMD(){
        LocalDate localeData= LocalDate.now();
        return localeData.format(DateTimeFormatter.ofPattern("yMMdd"));
    }


    /**
     * 获取新的文件名
     * @param file
     * @return
     */

    String getNewFileName(MultipartFile file){
//获取原始文件名
        String name=file.getOriginalFilename();
        //获取文件的后缀   把.以后的截取出来
        String ext=name.substring(name.indexOf("."));

        //新文件名   System.currentTimeMillis() 系统的毫米时间
        String newFile= UUID.randomUUID().toString() +ext;
//上传
        return  newFile;
    }


}
